package com.atguigu.gulimall.seckill.controller;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.seckill.serivce.SeckillService;
import com.atguigu.gulimall.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SeckillController {
    @Autowired
    SeckillService seckillService;
    /**
     * 获取当前时间可以参与秒杀的商品信息
     */
    @ResponseBody //c319 2:43
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus(){
        List<SeckillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();
        return R.ok().setData(vos);
    }

    /**
     * 获取当前商品的秒杀信息
     * @param skuId
     * @return
     */
    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){
        SeckillSkuRedisTo to = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(to);
    }

    /**
     * 秒杀业务
     * @param killId  is  5_2 7_1这些信息即 秒杀场次_对应的商品
     * @param key  这个是前台传来的 code=${item.seckillInfo.randomCode} 那怎么会和后面redis中的一样呢?
     * @param num
     * @return
     */
    @GetMapping("/kill")
    public String seckill(@RequestParam("killId")String killId,
                     @RequestParam("key")String key,
                     @RequestParam("num")Integer num,
                          Model model){
        //1、判断是否登录  -- 拦截器处理
        //秒杀成功 则返回订单号
        String order_sn = seckillService.kill(killId,key,num);
        model.addAttribute("orderSn",order_sn);
        return "success";
    }
}
